package com.lvy.hczn.transmit.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.core.domain.entity.SysRole;
import com.lvy.hczn.front.common.core.domain.entity.SysUser;
import com.lvy.hczn.front.system.domain.system.SupplierBoxPrefix;
import com.lvy.hczn.front.system.service.ISysRoleService;
import com.lvy.hczn.front.system.service.ISysUserService;
import com.lvy.hczn.front.system.service.SupplierBoxPrefixService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.front.common.core.page.TableDataInfo;

/**
 * 供应商货箱前缀管理Controller
 *
 * @author ruoyi
 * @date 2023-05-18
 */
@Controller
@RequestMapping("/system/prefix")
public class SupplierBoxPrefixController extends BaseController {
    private String prefix = "system/prefix";

    @Autowired
    private SupplierBoxPrefixService supplierBoxPrefixService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @RequiresPermissions("system:prefix:view")
    @GetMapping()
    public String prefix() {
        return prefix + "/prefix";
    }

    /**
     * 查询供应商货箱前缀管理列表
     */
    @RequiresPermissions("system:prefix:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SupplierBoxPrefix supplierBoxPrefix) {
        startPage();
        List<SupplierBoxPrefix> list = supplierBoxPrefixService.selectSupplierBoxPrefixList(supplierBoxPrefix);
        return getDataTable(list);
    }

    /**
     * 导出供应商货箱前缀管理列表
     */
    @RequiresPermissions("system:prefix:export")
    @Log(title = "供应商货箱前缀管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SupplierBoxPrefix supplierBoxPrefix) {
        List<SupplierBoxPrefix> list = supplierBoxPrefixService.selectSupplierBoxPrefixList(supplierBoxPrefix);
        ExcelUtil<SupplierBoxPrefix> util = new ExcelUtil<SupplierBoxPrefix>(SupplierBoxPrefix.class);
        return util.exportExcel(list, "供应商货箱前缀管理数据");
    }

    /**
     * 新增供应商货箱前缀管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存供应商货箱前缀管理
     */
    @RequiresPermissions("system:prefix:add")
    @Log(title = "供应商货箱前缀管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SupplierBoxPrefix supplierBoxPrefix) {
        return toAjax(supplierBoxPrefixService.insertSupplierBoxPrefix(supplierBoxPrefix));
    }

    /**
     * 修改供应商货箱前缀管理
     */
    @RequiresPermissions("system:prefix:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        SupplierBoxPrefix supplierBoxPrefix = supplierBoxPrefixService.selectSupplierBoxPrefixById(id);
        mmap.put("supplierBoxPrefix", supplierBoxPrefix);
        List<SysUser> sysUsers = new ArrayList<>();
        SysRole sysRole = new SysRole();
        sysRole.setRoleKey(Constants.ROLE_SUPPLIER_BOX_GEN);
        List<SysRole> list = sysRoleService.selectRoleList(sysRole);
        if (list != null && !list.isEmpty()) {
            SysUser sysUser = new SysUser();
            sysUser.setRoleId(list.get(0).getRoleId());
            sysUsers = sysUserService.selectAllocatedList(sysUser);
        }
        mmap.put("list", sysUsers);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商货箱前缀管理
     */
    @RequiresPermissions("system:prefix:edit")
    @Log(title = "供应商货箱前缀管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SupplierBoxPrefix supplierBoxPrefix) {
        return toAjax(supplierBoxPrefixService.updateSupplierBoxPrefix(supplierBoxPrefix));
    }

    /**
     * 删除供应商货箱前缀管理
     */
    @RequiresPermissions("system:prefix:remove")
    @Log(title = "供应商货箱前缀管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(supplierBoxPrefixService.deleteSupplierBoxPrefixByIds(ids));
    }

    /**
     * 校验前缀是否存在
     *
     * @param supplierBoxPrefix
     * @return boolean
     * @author yw
     * @date 2023-05-18 14:19:08
     */
    @PostMapping("/checkPrefixUnique")
    @ResponseBody
    public boolean checkPrefixUnique(SupplierBoxPrefix supplierBoxPrefix) {
        return supplierBoxPrefixService.checkPrefixUnique(supplierBoxPrefix);
    }
}
